<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="姓名/手机号" prop="user_name">
        <el-input v-model="queryParams.user_name" placeholder="请输入姓名/手机号" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="担保编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入担保编号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同号" prop="contract_code">
        <el-input
          v-model="queryParams.contract_code"
          placeholder="请输入合同号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:post:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="担保编号" align="center" prop="id" /> -->
      <el-table-column label="姓名" align="center" prop="user_name" />
      <!-- <el-table-column
        label="合同号"
        align="center"
        prop="contract_code"
        width="180"
      /> -->
      <el-table-column label="当前期次" align="center" prop="stages_phase" />
      <el-table-column label="应收本金" align="center" prop="ctd_principal_amt" />
      <el-table-column label="应收利息" align="center" prop="ctd_interesta_amt" />
      <el-table-column label="担保管理费" align="center" width="160" prop="manage_amt" />
      <el-table-column label="担保类型" align="center">
        <template #default="scope">
          <span>{{ loanTypes(scope.row.loan_type) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本期还款日" align="center" width="160" prop="pmt_due_date">
        <template #default="scope">
          <span>{{ scope.row.postSort5 }}</span
          ><br />
          <el-text class="mx-1" type="warning">即将到期</el-text>
        </template>
      </el-table-column>
      <el-table-column label="提醒次数" align="center" prop="num" />
      <el-table-column label="操作" fixed="right" width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip content="查看" placement="top">
            <el-button link type="primary" icon="View" @click="handelDetail(scope.row)" v-hasPermi="['system:post:edit']"></el-button>
          </el-tooltip>

          <el-tooltip content="到期提醒" placement="top">
            <el-button link type="primary" icon="Finished" @click="handleRemind(scope.row)" v-hasPermi="['system:post:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page_size" v-model:limit="queryParams.page_count" @pagination="getList" />

    <!-- 到期提醒 -->
    <el-dialog :title="`即将到期提醒(客户 ${item.user_name})`" v-model="open" width="500px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="通知方式" prop="checkList">
          <el-checkbox-group v-model="form.checkList">
            <el-checkbox label="系统推送" value="1" />
            <el-checkbox label="短信" value="2" />
            <el-checkbox label="邮件" value="3" />
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="使用模板" prop="temp">
          <el-input v-model="form.temp" placeholder="请输入编码名称" />
        </el-form-item>
        <el-form-item label="提醒周期" prop="cycle">
          <el-radio-group v-model="form.cycle">
            <el-radio label="1">一次</el-radio>
            <el-radio label="2">到期前每一天</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="发送时间" prop="send" v-if="form.cycle == 1">
          <el-date-picker v-model="form.send_time" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"> </el-date-picker>
        </el-form-item>
        <el-form-item label="发送时间" prop="send" v-if="form.cycle == 2">
          <el-select v-model="form.send_time">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Soon">
import { methodType, loanTypes } from "@/utils/dict";
import { list, add } from "@/api/collection/soon";
import { useRouter } from "vue-router";

const { proxy } = getCurrentInstance();
const router = useRouter();

const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const item = ref({});

const options = [
  {
    value: "00:00",
    label: "00:00",
  },
  {
    value: "01:00",
    label: "01:00",
  },
  {
    value: "02:00",
    label: "02:00",
  },
  {
    value: "03:00",
    label: "03:00",
  },
  {
    value: "04:00",
    label: "04:00",
  },
  {
    value: "05:00",
    label: "05:00",
  },
  {
    value: "06:00",
    label: "06:00",
  },
  {
    value: "07:00",
    label: "07:00",
  },
  {
    value: "08:00",
    label: "08:00",
  },
  {
    value: "09:00",
    label: "09:00",
  },
  {
    value: "10:00",
    label: "10:00",
  },
  {
    value: "11:00",
    label: "11:00",
  },
  {
    value: "12:00",
    label: "12:00",
  },
  {
    value: "13:00",
    label: "13:00",
  },
  {
    value: "14:00",
    label: "14:00",
  },
  {
    value: "15:00",
    label: "15:00",
  },
  {
    value: "16:00",
    label: "16:00",
  },
  {
    value: "17:00",
    label: "17:00",
  },
  {
    value: "18:00",
    label: "18:00",
  },
  {
    value: "19:00",
    label: "19:00",
  },
  {
    value: "20:00",
    label: "20:00",
  },
  {
    value: "21:00",
    label: "21:00",
  },
  {
    value: "22:00",
    label: "2:00",
  },
  {
    value: "23:00",
    label: "23:00",
  },
];

const data = reactive({
  form: {
    checkList: [],
    temp: "",
    cycle: "1",
    send: "",
    send_time: "",
  },
  queryParams: {
    page_count: 10,
    page_size: 1,
    user_name: "",
    contract_code: "",
    id: "",
  },
  rules: {
    checkList: [{ required: true, message: "通知方式必须选择", trigger: "blur" }],
    temp: [{ required: true, message: "使用模板不能为空", trigger: "blur" }],
    cycle: [{ required: true, message: "提醒周期不能为空", trigger: "blur" }],
    send_time: [{ required: true, message: "发送时间不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  console.log(queryParams.value, "queryParams.value");
  list({ ...queryParams.value, user_phone: queryParams.value.user_name }).then((response) => {
    postList.value = response.data.list;
    total.value = response.data.total_page;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    checkList: [],
    temp: "",
    cycle: "1",
    send: "",
    send_time: "",
  };
  proxy.resetForm("postRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page_count = 10;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.user_name = "";
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 到期提醒 */
function handleRemind(value) {
  item.value = value;
  reset();
  open.value = true;
}
/** 修改按钮操作 */
function handelDetail(row) {
  router.push({
    path: "/collection/soon-detail",
    state: {
      infos: {
        id: row.id,
        apply_id: row.apply_id,
        purpose_name: row.purpose_name,
        debit_card_id: row.debit_card_id,
        user_name: row.user_name,
        approval_rate: row.approval_rate,
        loan_type_classify: loanTypes(row.loan_type).slice(0, 2),
        loan_type_name: loanTypes(row.loan_type).slice(2, 5),
        loan_stages: row.loan_stages + "个月",
        apply_quota: !row.apply_quota ? row.apply_quota : row.apply_quota + "元",
        method_type: methodType(row.method),
        contract_amount: !row.contract_amount ? row.contract_amount : row.contract_amount + "元",
        approval_amount: !row.approval_amount ? row.approval_amount : row.approval_amount + "元",
      },
    },
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      add({
        apply_id: item.value.apply_id,
        detail_id: item.value.id,
        ...form.value,
        send: form.value.cycle == 1 ? 1 : 2,
        method: form.value.checkList.join(","),
      }).then((response) => {
        proxy.$modal.msgSuccess("即将到期提醒添加成功");
        open.value = false;
        getList();
      });
    }
  });
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download("loan/export/becomeDueList", {
    ...queryParams.value,
    user_phone: queryParams.value.user_name,
  });
}

getList();
</script>
